# Maintainer: Felix Yan <felixonmars@archlinux.org>
# Contributor: Sascha Weaver <wzyboy@wzyboy.org>

# This font has four versions:
#
# * Multilingual versions with a default language: (OTF)
#   (We do not package them)
#   * Multilingual version with Japanese glyphs as default priority
#   * Multilingual version with Korean glyphs as default priority
#   * Multilingual version with Simplified Chinese glyphs as default priority
#   * Multilingual version with Traditional Chinese glyphs as default priority
# * Multilingual version combined the four versions above. (OTC) (Not included in the tarball anymore)
#   (This version is "the best of the worlds", but it cannot be used in Windows)
#   -> adobe-source-han-sans-otc-fonts
# * Multilingual version compressed the 7 files into a single ttc file. (SuperOTC)
#   (We package them now, as there's no difference from the OTC version above)
# * Regional subsets with only characters used in CN, JP, KR or TW: (SubsetOTF)
#   (This version is smaller and has better compatibility)
#   * Regional subset with only Japanese glyphs + Latin
#   -> adobe-source-han-sans-jp-fonts
#   * Regional subset with only Korean glyphs + Latin
#   -> adobe-source-han-sans-kr-fonts
#   * Regional subset with only Simplified Chinese glyphs + Latin
#   -> adobe-source-han-sans-cn-fonts
#   * Regional subset with only Traditional Chinese glyphs + Latin
#   -> adobe-source-han-sans-tw-fonts

_pkgbase=adobe-source-han-sans
pkgbase=$_pkgbase-fonts
pkgname=($_pkgbase-jp-fonts $_pkgbase-kr-fonts $_pkgbase-cn-fonts $_pkgbase-tw-fonts $_pkgbase-hk-fonts $_pkgbase-otc-fonts)
pkgver=2.004
pkgrel=1
pkgdesc='Adobe Source Han Sans - A set of Pan-CJK fonts designed to complement Source Sans Pro'
arch=('any')
license=('custom:SIL')
url='https://github.com/adobe-fonts/source-han-sans'
source=("$pkgbase-$pkgver.tar.gz::https://github.com/adobe-fonts/source-han-sans/archive/${pkgver}R.tar.gz"
        44-source-han-sans-jp.conf
        44-source-han-sans-kr.conf
        44-source-han-sans-cn.conf
        44-source-han-sans-tw.conf
        44-source-han-sans-hk.conf
        44-source-han-sans-otc.conf)
sha512sums=('f8fe85b5e62445ff6d2e07daff414b0fec1334840fb308fb06407838badb95df7bbdcae24a7e4b6df9a5c2473f72633cbef235a1bfac22f0cdb65217d47e10c1'
            '2582097792c2938cf8a04a21795acf6d6640531a8509865111bfbaeb41ce96a632d8f694aae93aa68f375a9f496a3eb8ecd6f6909f81045e2a0a42b432898588'
            '53aba437f7eddb89826ec1050840081f8f509f860b9bc43c71a7b1a5d294fbc6256a7b8690f9a28b505bc2be63bf77bcf6bf8ae93d7a4783d7f91d0305d84147'
            'fd1fd6a5cf68a879bd5286642427ba6a01944568106ef186a18942bd0db658e61da8656a23a83468f266e4ec332baa71485a3d91f01333d48cc840909ac3fd5d'
            '430d0a5e86fdf5c89ca2d12886901dc3dde3bb6f5d7be494e6b404bb0040dc11e591fd642f23e0d92784e8f01d7e27cd90aa2805f4ca6ea2be401405a36a1b20'
            '40bbac5de07abece7aae291c368325a421a187bf2742f0dc0028ff5722ac2c655a8c854a4371746d76917652921ee7e8e0c96f6635fd48c5238b521b42dcb961'
            '14342eb8d9eab434b325100707376c28c584a6b7ff3a7b7b67fbc1bf5dbd1918b50000fe47101ee9ca42f60d9272c16fbc1abed5090dbab7b704c10d48a3920b')

_jp=(
SourceHanSansJP-Bold.otf
SourceHanSansJP-ExtraLight.otf
SourceHanSansJP-Heavy.otf
SourceHanSansJP-Light.otf
SourceHanSansJP-Medium.otf
SourceHanSansJP-Normal.otf
SourceHanSansJP-Regular.otf
)
_kr=(
SourceHanSansKR-Bold.otf
SourceHanSansKR-ExtraLight.otf
SourceHanSansKR-Heavy.otf
SourceHanSansKR-Light.otf
SourceHanSansKR-Medium.otf
SourceHanSansKR-Normal.otf
SourceHanSansKR-Regular.otf
)
_cn=(
SourceHanSansCN-Bold.otf
SourceHanSansCN-ExtraLight.otf
SourceHanSansCN-Heavy.otf
SourceHanSansCN-Light.otf
SourceHanSansCN-Medium.otf
SourceHanSansCN-Normal.otf
SourceHanSansCN-Regular.otf
)
_tw=(
SourceHanSansTW-Bold.otf
SourceHanSansTW-ExtraLight.otf
SourceHanSansTW-Heavy.otf
SourceHanSansTW-Light.otf
SourceHanSansTW-Medium.otf
SourceHanSansTW-Normal.otf
SourceHanSansTW-Regular.otf
)
_hk=(
SourceHanSansHK-Bold.otf
SourceHanSansHK-ExtraLight.otf
SourceHanSansHK-Heavy.otf
SourceHanSansHK-Light.otf
SourceHanSansHK-Medium.otf
SourceHanSansHK-Normal.otf
SourceHanSansHK-Regular.otf
)
_otc=(
SourceHanSans.ttc
)

function _package {
    cd "$srcdir/source-han-sans-${pkgver}R"

    case "$1" in
        $_pkgbase-jp-fonts)
            fonts=(${_jp[@]})
            cd SubsetOTF/JP
            _fontconfig_filename=44-source-han-sans-jp.conf
            pkgdesc="Adobe Source Han Sans Subset OTF - Japanese OpenType/CFF fonts";;
        $_pkgbase-kr-fonts)
            fonts=(${_kr[@]})
            cd SubsetOTF/KR
            _fontconfig_filename=44-source-han-sans-kr.conf
            pkgdesc="Adobe Source Han Sans Subset OTF - Korean OpenType/CFF fonts";;
        $_pkgbase-cn-fonts)
            fonts=(${_cn[@]})
            cd SubsetOTF/CN
            _fontconfig_filename=44-source-han-sans-cn.conf
            pkgdesc="Adobe Source Han Sans Subset OTF - Simplified Chinese OpenType/CFF fonts";;
        $_pkgbase-tw-fonts)
            fonts=(${_tw[@]})
            cd SubsetOTF/TW
            _fontconfig_filename=44-source-han-sans-tw.conf
            pkgdesc="Adobe Source Han Sans Subset OTF - Traditional Chinese (Taiwan) OpenType/CFF fonts";;
        $_pkgbase-hk-fonts)
            fonts=(${_hk[@]})
            cd SubsetOTF/HK
            _fontconfig_filename=44-source-han-sans-hk.conf
            pkgdesc="Adobe Source Han Sans Subset OTF - Traditional Chinese (Hong Kong) OpenType/CFF fonts";;
        $_pkgbase-otc-fonts)
            fonts=(${_otc[@]})
            cd SuperOTC
            bsdtar xf SourceHanSans.ttc.zip
            _fontconfig_filename=44-source-han-sans-otc.conf
            pkgdesc="Adobe Source Han Sans - Pan-CJK OpenType/CFF Collection fonts";;
    esac

    # Prepare destination directory
    install -dm755 "$pkgdir/usr/share/fonts/adobe-source-han-sans"

    # Install fonts
    for font in "${fonts[@]}"; do
        install -m644 "$font" "$pkgdir/usr/share/fonts/adobe-source-han-sans"
    done

    # Install fontconfig fix
    install -d "$pkgdir/usr/share/fontconfig/conf.default"
    install -Dm644 "$srcdir/$_fontconfig_filename" "$pkgdir/usr/share/fontconfig/conf.avail/$_fontconfig_filename"
    ln -s ../conf.avail/$_fontconfig_filename "$pkgdir/usr/share/fontconfig/conf.default/$_fontconfig_filename"

    install -Dm644 "$srcdir"/source-han-sans-${pkgver}R/LICENSE.txt -t "$pkgdir"/usr/share/licenses/$1/
}

for _pkgname in ${pkgname[@]}; do
    eval "function package_$_pkgname() { _package $_pkgname; }"
done
